package com.tokenplus.thai.create.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.tokenplus.thai.R

class MnemonicVerifyAdapter(val context: Context, val dataList: List<DataMnemonicVerify>) :
    RecyclerView.Adapter<MnemonicVerifyAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var textAdpterMnemonicVerifyIndex: TextView =
            view.findViewById(R.id.textAdpterMnemonicVerifyIndex)
        var listAdpterMnemonicVerify: RecyclerView =
            view.findViewById(R.id.listAdpterMnemonicVerify)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_mnemonic_verify, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: DataMnemonicVerify = dataList[position]
        if (data.index > 9) {
            holder.textAdpterMnemonicVerifyIndex.text = data.index.toString()
        } else {
            holder.textAdpterMnemonicVerifyIndex.text = "0${data.index}"
        }

        val mMnemonicVerifyStatusAdapter = MnemonicVerifyStatusAdapter(data.list)
        mMnemonicVerifyStatusAdapter.setItemClickListener { clickIndex ->
            for (index in 0 until data.list.size) {
                val item = data.list[index]
                item.selected = (index == clickIndex)
            }
            mMnemonicVerifyStatusAdapter.notifyDataSetChanged()
        }

        holder.listAdpterMnemonicVerify.layoutManager = GridLayoutManager(context, 3)
        holder.listAdpterMnemonicVerify.adapter = mMnemonicVerifyStatusAdapter
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}